package com.example.yan_music.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.yan_music.R;
import com.example.yan_music.base.fragment.BaseFragment;
import com.example.yan_music.contract.ILoginContract;
import com.example.yan_music.presenter.ILoginPresenterImp;
import com.example.yan_music.view.MainActivity;
import com.example.yan_music.widge.dialog.SingleDialog;
import com.orhanobut.logger.Logger;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * <pre>
 *     登录页面，实现了登录页面VP仓库里的接口
 * </pre>
 */
public class LoginFragment extends BaseFragment implements ILoginContract.View {

    private static final String TAG = "LoginFragment";

    // 对应P层的对象
    private ILoginPresenterImp loginPresenter;

    // 控件
    private EditText edt_username, edt_password;
    private TextView tv_register;
    private Button bt_login;
    private CircleImageView img_user;

    // 所在的主活动
    private MainActivity mActivity;

    // 判断登录是否成功
    private boolean isLoginSucceed;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        init(view);
        return view;
    }

    /**
     * 初始化操作
     *
     * @param view
     */
    @Override
    public void init(View view) {
        super.init(view);
        // 将此View和对应的Presenter关联起来，不然用mView调用会报空
        loginPresenter = new ILoginPresenterImp();
        loginPresenter.attachView(this);

        edt_username = view.findViewById(R.id.edt_username);
        edt_password = view.findViewById(R.id.edt_password);
        tv_register = view.findViewById(R.id.tv_register);
        bt_login = view.findViewById(R.id.bt_login);

        bt_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

    /**
     * 获取用户名密码
     *
     * @return
     */
    @Override
    public String getUserName() {
        Logger.d(edt_username.getText().toString());
        return edt_username.getText().toString();
    }

    /**
     * 获取输入的密码
     *
     * @return
     */
    @Override
    public String getUserPassword() {
        Logger.d(edt_password.getText().toString());
        return edt_password.getText().toString();
    }

    /**
     * 登录成功后，提示用户登录成功并跳转到已经成功登录后的主活动
     */
    @Override
    public void onLoginSucceed() {

    }

    /**
     * 登录失败，弹出“登录失败”对话框
     */
    @Override
    public void onLoginFailed() {

    }

    /**
     * 这里从MainFragment跳转来的时候隐藏MainActivity的player视图
     *
     * @param context
     */
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (MainActivity) context;
        Logger.d("player_layout隐藏");

    }

    /**
     * 销毁时显示player视图显示出来
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("player_layout显示");

    }

    @Override
    public void showLoadingDialog() {
        SingleDialog singleDialog = new SingleDialog(mActivity, R.style.single_dialog);
        singleDialog.setCancelable(false);
        singleDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            // 点击登录按钮
            case R.id.bt_login:
                loginPresenter.login(getUserName(), getUserPassword());
                break;

            // TODO 点击注册按钮
            case R.id.tv_register:
                //loginPresenter.register();
                break;

            default:
                break;
        }
    }
}